<!DOCTYPE html>
<html>

<head>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
	<meta name="theme-color" content="#33474d">
	<title>rc.local自启动学习 | 失落的乐章</title>
	<link rel="stylesheet" href="/css/style.css" />
	
      <link rel="alternate" href="/atom.xml" title="失落的乐章" type="application/atom+xml">
    
</head>

<body>

	<header class="header">
		<nav class="header__nav">
			
				<a href="/archives" class="header__link">Archive</a>
			
				<a href="/tags" class="header__link">Tags</a>
			
				<a href="/atom.xml" class="header__link">RSS</a>
			
		</nav>
		<h1 class="header__title"><a href="/">失落的乐章</a></h1>
		<h2 class="header__subtitle">技术面前，永远都是学生。</h2>
	</header>

	<main>
		<article>
	
		<h1>rc.local自启动学习</h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/Linux/">Linux</a>
			</span>
		
	</div>

	

	
		<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;linux有自己一套完整的启动体系，抓住了linux启动的脉络，linux的启动过程将不再神秘。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;本文中假设inittab中设置的init tree为：</p>
<ul>
<li>/etc/rc.d/rc0.d</li>
<li>/etc/rc.d/rc1.d</li>
<li>/etc/rc.d/rc2.d</li>
<li>/etc/rc.d/rc3.d</li>
<li>/etc/rc.d/rc4.d</li>
<li>/etc/rc.d/rc5.d</li>
<li>/etc/rc.d/rc6.d</li>
<li>/etc/rc.d/init.d</li>
</ul>
<h2 id="1-关于linux的启动"><a href="#1-关于linux的启动" class="headerlink" title="1. 关于linux的启动"></a>1. 关于linux的启动</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;init是所有进程的顶层</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;init读取/etc/inittab，执行rc.sysinit脚本(注意文件名是不一定的,有些unix甚至会将语句直接写在inittab中)</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;rc.sysinit脚本作了很多工作:</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div></pre></td><td class="code"><pre><div class="line">init <span class="variable">$PATH</span></div><div class="line">config network</div><div class="line">start swap <span class="keyword">function</span></div><div class="line"><span class="built_in">set</span> hostname</div><div class="line">check root file system, repair <span class="keyword">if</span> needed</div><div class="line">check root space</div><div class="line">....</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;rc.sysinit根据inittab执行rc?.d脚本</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;linux是多用户系统，getty是多用户与单用户的分水岭</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;在getty之前运行的是系统脚本</p>
<h2 id="2-关于rc-d"><a href="#2-关于rc-d" class="headerlink" title="2. 关于rc.d"></a>2. 关于rc.d</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;所有启动脚本放置在 /etc/rc.d/init.d下</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;rc?.d中放置的是init.d中脚本的链接，命名格式是:</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">S&#123;number&#125;&#123;name&#125;</div><div class="line">K&#123;number&#125;&#123;name&#125;</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;S开始的文件向脚本传递start参数</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;K开始的文件向脚本传递stop参数</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;number决定执行的顺序</p>
<h2 id="3-启动脚本示例"><a href="#3-启动脚本示例" class="headerlink" title="3. 启动脚本示例"></a>3. 启动脚本示例</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;这是一个用来启动httpd的 /etc/rc.d/init.d/apache 脚本：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line"><span class="meta">#!/bin/bash</span></div><div class="line">......</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;可以看出他接受start,stop,restart,status参数</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;然后可以这样建立rc?.d的链接：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div></pre></td><td class="code"><pre><div class="line"><span class="built_in">cd</span> /etc/rc.d/init.d &amp;&amp;</div><div class="line">ln -sf ../init.d/apache ../rc0.d/K28apache &amp;&amp;</div><div class="line">ln -sf ../init.d/apache ../rc1.d/K28apache &amp;&amp;</div><div class="line">ln -sf ../init.d/apache ../rc2.d/K28apache &amp;&amp;</div><div class="line">ln -sf ../init.d/apache ../rc3.d/S32apache &amp;&amp;</div><div class="line">ln -sf ../init.d/apache ../rc4.d/S32apache &amp;&amp;</div><div class="line">ln -sf ../init.d/apache ../rc5.d/S32apache &amp;&amp;</div><div class="line">ln -sf ../init.d/apache ../rc6.d/K28apache</div></pre></td></tr></table></figure>
<h2 id="4-关于rc-local"><a href="#4-关于rc-local" class="headerlink" title="4. 关于rc.local"></a>4. 关于rc.local</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;经常使用的 rc.local 则完全是习惯问题，不是标准。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;各个发行版有不同的实现方法，可以这样实现：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div></pre></td><td class="code"><pre><div class="line">touch /etc/rc.d/rc.local</div><div class="line">chmod +x /etc/rc.d/rc.local</div><div class="line">ln -sf /etc/rc.d/rc.local /etc/rc.d/rc1.d/S999rc.local &amp;&amp;</div><div class="line">ln -sf /etc/rc.d/rc.local /etc/rc.d/rc2.d/S999rc.local &amp;&amp;</div><div class="line">ln -sf /etc/rc.d/rc.local /etc/rc.d/rc3.d/S999rc.local &amp;&amp;</div><div class="line">ln -sf /etc/rc.d/rc.local /etc/rc.d/rc4.d/S999rc.local &amp;&amp;</div><div class="line">ln -sf /etc/rc.d/rc.local /etc/rc.d/rc5.d/S999rc.local &amp;&amp;</div><div class="line">ln -sf /etc/rc.d/rc.local /etc/rc.d/rc6.d/S999rc.local</div></pre></td></tr></table></figure>
<h2 id="5-关于bash启动脚本"><a href="#5-关于bash启动脚本" class="headerlink" title="5. 关于bash启动脚本"></a>5. 关于bash启动脚本</h2><ul>
<li>/etc/profile</li>
<li>/etc/bashrc</li>
<li>~/.bash_profile</li>
<li>~/.bashrc</li>
</ul>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;是bash的启动脚本</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;一般用来设置单用户的启动环境，也可以实现开机单用户的程序，但要明确他们都是属于bash范畴而不是系统范畴。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;它们的具体作用介绍如下：</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;/bin/bash这个命令解释程序(后面简称shell)使用了一系列启动文件来建立一个运行环境：</p>
<ul>
<li>/etc/profile</li>
<li>/etc/bashrc</li>
<li>~/.bash_profile</li>
<li>~/.bashrc</li>
<li>~/.bash_logout</li>
</ul>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;每一个文件都有特殊的功用并对登陆和交互环境有不同的影响。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;/etc/profile 和 ~/.bash_profile 是在启动一个交互登陆shell的时候被调用。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;/etc/bashrc 和 ~/.bashrc 是在一个交互的非登陆shell启动的时候被调用。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;~/.bash_logout 在用户注销登陆的时候被读取</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;一个交互的登陆shell会在 /bin/login 成功登陆之后运行。一个交互的非登陆shell是通过命令行来运行的，如[prompt]$/bin/bash。一般一个非交互的shell出现在运行 shell脚本的时候。之所以叫非交互的shell，是因为它不在命令行上等待输入而只是执行脚本程序。</p>
<h2 id="6-关于开机程序的自动启动"><a href="#6-关于开机程序的自动启动" class="headerlink" title="6. 关于开机程序的自动启动"></a>6. 关于开机程序的自动启动</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;系统脚本可以放置在/etc/rc.d/init.d中并建立/etc/rc.d/rc?.d链接，也可以直接放置在/etc/rc.d/rc.local中。</p>
<hr>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;init.d脚本包含完整的start,stop,status,reload等参数，是标准做法，推荐使用。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;为特定用户使用的程序（如有的用户需要使用中文输入法而有的不需要）放置在~/中的bash启动脚本中。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;设置系统自动启动<br>在/etc/init.d/下创建smsafe文件<br>内容：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div></pre></td><td class="code"><pre><div class="line"><span class="meta">#!/bin/bash</span></div><div class="line"><span class="comment"># chkconfig: 35 95 1</span></div><div class="line"><span class="comment"># description: script to start/stop smsafe</span></div><div class="line"><span class="keyword">case</span> 1instart)sh/opt/startsms.sh;;stop)sh/opt/stopsms.sh;;∗)<span class="built_in">echo</span><span class="string">"Usage: 0 (start|stop)"</span></div><div class="line">;;</div><div class="line"><span class="keyword">esac</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;更改权限</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line"><span class="comment"># chmod 775 smsafe</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;加入自动启动</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line"><span class="comment"># chkconfig –add smsafe</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;查看自动启动设置</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line"><span class="comment"># chkconfig –list smsafe</span></div><div class="line">smsafe 0:off 1:off 2:off 3:on 4:off 5:on 6:off</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;以后可以用以下命令启动和停止脚本</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line"><span class="comment"># service smsafe start 启动</span></div><div class="line"><span class="comment"># service smsafe stop 停止</span></div></pre></td></tr></table></figure>
<hr>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;jira 的启动主要依靠的是bin目录下的catalina.sh脚本，提供了如init脚本的start，stop等参数</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div></pre></td><td class="code"><pre><div class="line"><span class="meta">#!/bin/bash</span></div><div class="line"><span class="comment">#</span></div><div class="line"><span class="comment"># chkconfig: 2345 85 15</span></div><div class="line"><span class="comment"># description: jira</span></div><div class="line"><span class="comment"># processname: jira</span></div><div class="line"><span class="comment"># source function library</span></div><div class="line">. /etc/init.d/<span class="built_in">functions</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;下面一行比较重要，为jira的安装路径，没有的话，将会提示找不到文件</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div></pre></td><td class="code"><pre><div class="line">CATALINA_HOME=<span class="string">"/var/www/jira"</span></div><div class="line">RETVAL=0</div><div class="line"><span class="function"><span class="title">start</span></span>() &#123;</div><div class="line"><span class="built_in">echo</span> -n $<span class="string">"Starting jira services: "</span></div><div class="line">. /var/www/jira/bin/catalina.sh start</div><div class="line">RETVAL=$?</div><div class="line"><span class="built_in">echo</span></div><div class="line">&#125;</div><div class="line"><span class="function"><span class="title">stop</span></span>() &#123;</div><div class="line"><span class="built_in">echo</span> -n $<span class="string">"Shutting down jira services: "</span></div><div class="line">. /var/www/jira/bin/catalina.sh stop</div><div class="line">RETVAL=$?</div><div class="line"><span class="built_in">echo</span></div><div class="line">&#125;</div><div class="line"><span class="keyword">case</span> <span class="string">"<span class="variable">$1</span>"</span> <span class="keyword">in</span></div><div class="line">start)</div><div class="line">start</div><div class="line">;;</div><div class="line">stop)</div><div class="line">stop</div><div class="line">;;</div><div class="line">restart|reload)</div><div class="line">stop</div><div class="line">start</div><div class="line">;;</div><div class="line">status)</div><div class="line">status jira</div><div class="line">RETVAL=$?</div><div class="line">;;</div><div class="line">*)</div><div class="line"><span class="built_in">echo</span> <span class="string">"Usage: 0 &#123;start|stop|restart|status&#125;"</span></div><div class="line"><span class="built_in">exit</span> 1</div><div class="line"><span class="keyword">esac</span></div><div class="line"><span class="built_in">exit</span> <span class="variable">$RETVAL</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;保存为/etc/init.d/jira<br>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;然后利用</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">chkconfig --add jira</div><div class="line">OK</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;启动</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">/etc/init.d/jira start</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;停止</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">/etc/init.d/jira stop</div></pre></td></tr></table></figure>
<hr>
<h3 id="以Websphere为例子"><a href="#以Websphere为例子" class="headerlink" title="以Websphere为例子"></a>以Websphere为例子</h3><ol>
<li>在/etc/rc.d/init.d目录下新建启动脚本startWebsphere，键入以下内容：</li>
</ol>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line"><span class="meta">#!/bin/sh</span></div><div class="line">/opt/WebSphere/AppServer/bin/startServer.sh server1</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;修改该文件的权限：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">chmod 755 startWebsphere</div></pre></td></tr></table></figure>
<ol>
<li>在对应的目录下建立软连接(假设系统默认进入X11)</li>
</ol>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line"><span class="built_in">cd</span> /etc/rc.d/rc5.d</div><div class="line">ln -s ../init.d/startWebsphere S99startWebsphere</div></pre></td></tr></table></figure>
<ol>
<li>重启系统即可</li>
</ol>
<hr>
<h3 id="linux下oracle的自启动脚本"><a href="#linux下oracle的自启动脚本" class="headerlink" title="linux下oracle的自启动脚本"></a>linux下oracle的自启动脚本</h3><ol>
<li>写一个StartOracle.sql,假设放在/目录下</li>
</ol>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line">vi /StartOracle.sql  <span class="comment">##加入如下两行保存</span></div><div class="line">startup</div><div class="line"><span class="built_in">exit</span></div></pre></td></tr></table></figure>
<p>2.配置/etc/rc.local</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">vi /etc/rc.local   <span class="comment">##加入如下内容，保存</span></div><div class="line">su - oracle -c <span class="string">'ORACLE H OME/bin/lsnrctlstart ′ su−oracle−c ′ ORACLE_HOME/bin/sqlplus "/as sysdba" @/StartOracle.sql'</span></div></pre></td></tr></table></figure>
<ol>
<li>如果还要自动启动oracle enterprise manager(em)和isqlplus可以如下配置</li>
</ol>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">vi /etc/rc.local     <span class="comment">##加入</span></div><div class="line">su - oracle -c <span class="string">'ORACLE H OME/bin/emctlstartdbconsole ′ su−oracle−c ′ ORACLE_HOME/bin/isqlplusctl start'</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;要知道em和isqlplus等使用的端口可以查询文件：</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;$ORACLE_HOME/install/portlist.ini(以oracle 10.1.0.3为例)</p>
<hr>

	

	
		<span class="different-posts"><a href="/2017/10/12/1. Linux 基础/63. rc.local自启动学习/" onclick="window.history.go(-1); return false;">⬅️ Go back </a></span>

	

</article>

	</main>

	<footer class="footer">
	<div class="footer-content">
		
	      <div class="footer__element">
	<p>Hi there, <br />welcome to my Blog glad you found it. Have a look around, will you?</p>
</div>

	    
	      <div class="footer__element">
	<h5>Check out</h5>
	<ul class="footer-links">
		<li class="footer-links__link"><a href="/archives">Archive</a></li>
		
		  <li class="footer-links__link"><a href="/atom.xml">RSS</a></li>
	    
		<li class="footer-links__link"><a href="/about">about page</a></li>
		<li class="footer-links__link"><a href="/tags">Tags</a></li>
		<li class="footer-links__link"><a href="/categories">Categories</a></li>
	</ul>
</div>

	    

		<div class="footer-credit">
			<span>© 2017 失落的乐章 | Powered by <a href="https://hexo.io/">Hexo</a> | Theme <a href="https://github.com/HoverBaum/meilidu-hexo">MeiliDu</a></span>
		</div>

	</div>


</footer>



</body>

</html>
